'use strict';
import React from 'react';
import {
    View,
    Text,
    Image,
    TouchableOpacity,
    TextInput,
    Platform,
    StyleSheet,
    Keyboard,
    ActivityIndicator
} from 'react-native';
import ToolBar from '../Components/ToolBar';
import ImagePicker from 'react-native-image-picker';
import Toast from '../Utils/toast';
import Global from '../Commom/GlobalConst';

const width = Global.Const.size.width;
import RuralLibraryAPIService from '../Commom/RuralLibraryAPIService';
import FoundAPIService from '../Commom/FoundAPIService';
const styles = StyleSheet.create({
    choiceImgBox: {
        flexDirection: 'row',
        flexWrap: 'wrap',
        alignItems: 'center',
        padding: 10,
        backgroundColor: 'white',
        marginBottom: 10
    },
    image: {
        width: (width - 40) / 4,
        height: (width - 40) / 4,
        marginRight: 5,
        marginTop: 5,
    },
});

//发布点评
class InitiatedHelpToBuy extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            choiceImages: [],
            imagePath: [],
            isComment: true,
            CommentId: '',
            number: [],
            inputContent: '',
            submitting: false,
        }
    }

    choiceImage() {
        if (this.state.imagePath.length == 8) {
            Toast.show("最多选择8张图片");
            return;
        }
        let options = {
            title: '请选择图片',
            cancelButtonTitle: '取消',
            chooseFromLibraryButtonTitle: '从手机相册选择',
            takePhotoButtonTitle: '从相机拍照获取',
            allowsEditing: true,
            maxWidth: 750,
            maxHeight: 1280,
            quality:0.75,
        };
        ImagePicker.showImagePicker(options, (res) => {
            if (res.didCancel) {
                Toast.show('已取消选择');
            } else if (res.error) {
                Toast.show('选择图片错误');
            } else if (res.customButton) {

            } else {
                let source = '';
                let path = '';
                if (Platform.OS === 'ios') {
                    source = {uri: res.uri.replace('file://', ''), isStatic: true};
                    path = res.uri.replace('file://', '');
                } else {
                    source = {uri: res.uri, isStatic: true};
                    path = res.path;
                }
                this.setState({
                    choiceImages: this.state.choiceImages.concat(source),
                    imagePath: this.state.imagePath.concat(path),
                });
            }
        });
    }


    release() {
        Keyboard.dismiss();
        if (!this.state.inputContent) {
            Toast.show("请输入点评内容");
            return;
        }
        this.setState({
            submitting: true,
        });
        let rid = this.props.rid;
        let type = this.props.type;
        if (type == 0) {
            RuralLibraryAPIService.comment(Global.Const.token, Global.Const.registeredId, rid, this.state.inputContent).then((ret)=> {
                if (this.state.imagePath.length > 0) {
                    for (var i = 0; i < this.state.imagePath.length; i++) {
                        RuralLibraryAPIService.collectionImg(Global.Const.token, Global.Const.registeredId, ret, this.state.imagePath[i]).then((ret)=> {
                            this.setState({
                                number: this.state.number.concat(i),
                            });
                            if (this.state.number.length == this.state.imagePath.length) {
                                this.setState({
                                    submitting: false,
                                });
                                Toast.show("发布成功");
                                this.props.refreshComments();
                                this.props.navigator.pop();
                            }
                        }).catch((err)=> {
                            this.setState({
                                submitting: false,
                            });
                        });
                    }
                } else {
                    this.setState({
                        submitting: false,
                    });
                    Toast.show("发布成功");
                    this.props.refreshComments();
                    this.props.navigator.pop();
                }
            }).catch((err)=> {
                this.setState({
                    submitting: false,
                });
                Toast.show("后台出错");
            });
        } else if (type == 1) {
            RuralLibraryAPIService.amuse_comment(Global.Const.token, Global.Const.registeredId, rid, this.state.inputContent).then((ret)=> {
                if (this.state.imagePath.length > 0) {
                    for (var i = 0; i < this.state.imagePath.length; i++) {
                        RuralLibraryAPIService.amuse_collectionImg(Global.Const.token, Global.Const.registeredId, ret, this.state.imagePath[i]).then((ret)=> {
                            this.setState({
                                number: this.state.number.concat(i),
                            });
                            if (this.state.number.length == this.state.imagePath.length) {
                                this.setState({
                                    submitting: false,
                                });
                                Toast.show("发布成功");
                                this.props.refreshComments();
                                this.props.navigator.pop();
                            }
                        }).catch((err)=> {
                            this.setState({
                                submitting: false,
                            });
                        });
                    }
                } else {
                    this.setState({
                        submitting: false,
                    });
                    Toast.show("发布成功");
                    this.props.refreshComments();
                    this.props.navigator.pop();
                }
            }).catch((err)=> {
                this.setState({
                    submitting: false,
                });
                Toast.show("后台出错");
            });
        } else if (type == 2) {
            RuralLibraryAPIService.people_comment(Global.Const.token, Global.Const.registeredId, rid, this.state.inputContent).then((ret)=> {
                if (this.state.imagePath.length > 0) {
                    for (var i = 0; i < this.state.imagePath.length; i++) {
                        RuralLibraryAPIService.people_collectionImg(Global.Const.token, Global.Const.registeredId, ret, this.state.imagePath[i]).then((ret)=> {
                            this.setState({
                                number: this.state.number.concat(i),
                            });
                            if (this.state.number.length == this.state.imagePath.length) {
                                this.setState({
                                    submitting: false,
                                });
                                Toast.show("发布成功");
                                this.props.refreshComments();
                                this.props.navigator.pop();
                            }
                        }).catch((err)=> {
                            this.setState({
                                submitting: false,
                            });
                        });
                    }
                } else {
                    this.setState({
                        submitting: false,
                    });
                    Toast.show("发布成功");
                    this.props.refreshComments();
                    this.props.navigator.pop();
                }
            }).catch((err)=> {
                this.setState({
                    submitting: false,
                });
                Toast.show("后台出错");
            });
        } else if (type == 3) {
            RuralLibraryAPIService.hotel_comment(Global.Const.token, Global.Const.registeredId, rid, this.state.inputContent).then((ret)=> {
                if (this.state.imagePath.length > 0) {
                    for (var i = 0; i < this.state.imagePath.length; i++) {
                        RuralLibraryAPIService.hotel_collectionImg(Global.Const.token, Global.Const.registeredId, ret, this.state.imagePath[i]).then((ret)=> {
                            this.setState({
                                number: this.state.number.concat(i),
                            });
                            if (this.state.number.length == this.state.imagePath.length) {
                                this.setState({
                                    submitting: false,
                                });
                                Toast.show("发布成功");
                                this.props.refreshComments();
                                this.props.navigator.pop();
                            }
                        }).catch((err)=> {
                            this.setState({
                                submitting: false,
                            });
                        });
                    }
                } else {
                    this.setState({
                        submitting: false,
                    });
                    Toast.show("发布成功");
                    this.props.refreshComments();
                    this.props.navigator.pop();
                }
            }).catch((err)=> {
                this.setState({
                    submitting: false,
                });
                Toast.show("后台出错");
            });
        } else if (type == 4) {
            RuralLibraryAPIService.people_shop_comment(Global.Const.token, Global.Const.registeredId, rid, this.state.inputContent).then((ret)=> {
                if (this.state.imagePath.length > 0) {
                    for (var i = 0; i < this.state.imagePath.length; i++) {
                        RuralLibraryAPIService.people_shopImg(Global.Const.token, Global.Const.registeredId, ret, this.state.imagePath[i]).then((ret)=> {
                            this.setState({
                                number: this.state.number.concat(i),
                            });
                            if (this.state.number.length == this.state.imagePath.length) {
                                this.setState({
                                    submitting: false,
                                });
                                Toast.show("发布成功");
                                this.props.refreshComments();
                                this.props.navigator.pop();
                            }
                        }).catch((err)=> {
                            this.setState({
                                submitting: false,
                            });
                        });
                    }
                } else {
                    this.setState({
                        submitting: false,
                    });
                    Toast.show("发布成功");
                    this.props.refreshComments();
                    this.props.navigator.pop();
                }
            }).catch((err)=> {
                this.setState({
                    submitting: false,
                });
                Toast.show("后台出错");
            });
        }else if (type == 5) {
            FoundAPIService.raiders_comment(Global.Const.token, Global.Const.registeredId, rid, this.state.inputContent).then((ret)=> {
                if (this.state.imagePath.length > 0) {
                    for (var i = 0; i < this.state.imagePath.length; i++) {
                        FoundAPIService.raiders_img(Global.Const.token, Global.Const.registeredId, ret, this.state.imagePath[i]).then((ret)=> {
                            this.setState({
                                number: this.state.number.concat(i),
                            });
                            if (this.state.number.length == this.state.imagePath.length) {
                                this.setState({
                                    submitting: false,
                                });
                                Toast.show("发布成功");
                                this.props.refreshComments();
                                this.props.navigator.pop();
                            }
                        }).catch((err)=> {
                            this.setState({
                                submitting: false,
                            });
                        });
                    }
                } else {
                    this.setState({
                        submitting: false,
                    });
                    Toast.show("发布成功");
                    this.props.refreshComments();
                    this.props.navigator.pop();
                }
            }).catch((err)=> {
                this.setState({
                    submitting: false,
                });
                Toast.show("后台出错");
            });
        }
    }


    render() {
        return (
            <TouchableOpacity activeOpacity={1} style={{flex: 1}} onPress={() => {
                Keyboard.dismiss();
            }}>
                <View style={{flex: 1, backgroundColor: '#F2F3F5'}}>
                    <ToolBar
                        leftFunc={()=> this.props.navigator.pop()}
                        title="发布点评"
                        rightFunc={() => {
                          this.release();
                        }}
                        rightImage={require('../../Image/release_icon.png')}
                    />
                    <View
                        style={{
                            padding: 10,
                            justifyContent: 'space-between',
                            marginBottom: 1,
                            backgroundColor: 'white',
                            marginTop: 10
                        }}>
                        <TextInput
                            textAlignVertical={'top'}
                            underlineColorAndroid={'rgba(0,0,0,0)'}
                            multiline={true}
                            placeholder={'请输入点评内容'}
                            maxLength={100}
                            returnKeyType={'go'}
                            value={this.state.inputContent}
                            onChangeText={inputContent => this.setState({ inputContent })}
                            style={{
                                fontSize: 15,
                                color: 'black',
                                lineHeight: 27,
                                height: 80,
                            }}
                        />
                    </View>
                    <View style={styles.choiceImgBox}>
                        {
                            this.state.choiceImages ?
                                this.state.choiceImages.map((item, index) => {
                                    return (
                                        <Image
                                            key={'image' + index}
                                            style={styles.image}
                                            source={item}
                                        />
                                    )
                                }) : null
                        }
                        <TouchableOpacity
                            onPress={() => this.choiceImage()}
                        >
                            <Image style={styles.image} source={require('../../Image/photo_input.png')}/>
                        </TouchableOpacity>
                    </View>
                </View>
                {this.state.submitting ? <View
                    style={{position: 'absolute',top:0,left:0,width:Global.Const.size.width,height:Global.Const.size.height,backgroundColor:null,alignItems:'center',justifyContent:'center'}}>
                    <View
                        style={{backgroundColor:'#333333',borderRadius:5, alignItems: 'center',justifyContent: 'center',width:80,height:80}}>
                        <ActivityIndicator
                            animating={this.state.submitting}
                            style={{ alignItems: 'center',justifyContent: 'center'}}
                            size="large"/>
                        <Text style={{color:'white'}}>正在提交</Text>
                    </View>
                </View> : null}
            </TouchableOpacity>
        );
    }
}

export default InitiatedHelpToBuy;